package com.weaver.qfengx.entity;

/**
 * 接口结果类
 * Created by YeShengtao on 2020/9/21 19:37
 */
public class Result {
    private String code;

    public String getCode() {
        return code;
    }

    public Result setCode(String code) {
        this.code = code;
        return this;
    }

    public String getMsg() {
        return msg;
    }

    public Result setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public Object getData() {
        return data;
    }

    public Result setData(Object data) {
        this.data = data;
        return this;
    }

    private String msg;
    private Object data;

    private static final String OK_CODE = "0";
    private static final String OK_MSG = "操作成功";
    private static final String ERR_CODE = "10005";
    private static final String ERR_MSG = "操作失败";
    private static final String TOKEN_ERR_CODE = "10001";
    private static final String TOKEN_ERR_MSG = "令牌不存在或未启用";
    private static final String PARAM_ERR_CODE = "10002";
    private static final String PARAM_ERR_MSG = "参数不正确";
    private static final String EXCEPTION_ERR_CODE = "10003";
    private static final String EXCEPTION_ERR_MSG = "发生异常";
    private static final String PERMISSION_ERR_CODE = "10004";
    private static final String PERMISSION_ERR_MSG = "没有相关权限";

    public static Result create(String code, String msg) {
        return new Result()
                .setCode(code)
                .setMsg(msg);
    }

    public static Result create(String code, String msg, Object data) {
        return new Result()
                .setCode(code)
                .setMsg(msg)
                .setData(data);
    }

    public static Result fail() {
        return create(ERR_CODE, ERR_MSG);
    }

    public static Result fail(String msg) {
        return create(ERR_CODE, msg);
    }

    public static Result ok() {
        return create(OK_CODE, OK_MSG);
    }

    public static Result ok(Object data) {
        return create(OK_CODE, OK_MSG, data);
    }

    public static Result noPermission() {
        return create(PERMISSION_ERR_CODE, PERMISSION_ERR_MSG);
    }

    public static Result exception() {
        return create(EXCEPTION_ERR_CODE, EXCEPTION_ERR_MSG);
    }
    public static Result exception(Object data) {
        return create(EXCEPTION_ERR_CODE, EXCEPTION_ERR_MSG, data);
    }


    public static Result tokenErr() {
        return create(TOKEN_ERR_CODE, TOKEN_ERR_MSG);
    }

    public static Result paramErr() {
        return create(PARAM_ERR_CODE, PARAM_ERR_MSG);
    }

    public boolean isOk() {
        return OK_CODE.equals(code);
    }

    @Override
    public String toString() {
        return "Result{" +
                "code='" + code + '\'' +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }
}
